package com.example.demo.dao;

import com.example.demo.model.Articleinfo;
import com.example.demo.model.DiscussInfo;
import com.example.demo.model.ReplyInfo;
import com.example.demo.model.vo.ArticleinfoVO;
import com.example.demo.model.vo.ReplyInfoVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {

    @Select("select * from articleinfo where uid = #{uid} order by id desc")
    List<Articleinfo> getListByUid(@Param("uid")int uid);


    @Delete("delete from articleinfo where id = #{aid} and uid = #{uid}")
    int del(@Param("aid")Integer aid,int uid);

    @Insert("insert into waitinfo(title,content,uid,user) values (#{title},#{content},#{uid},#{user})")
    int add(ArticleinfoVO articleinfoVO);

    @Select("select * from articleinfo where id = #{aid} and uid = #{uid}")
    Articleinfo getArticleByAndUid(@Param("aid")int aid, @Param("uid")int uid);

    @Update("update articleinfo set title = #{title}, content = #{content}, updatetime = #{updatetime} where id = #{id} and uid = #{uid}")
    int update(Articleinfo articleinfo);

    @Select("select * from articleinfo where id = #{aid}")
    Articleinfo getArticleById(@Param("aid")Integer aid);

    @Select("select count(*) from articleinfo where uid = #{uid}")
    int getArtCountByUid(@Param("uid")int uid);


    @Update("update articleinfo set rcount = rcount + 1 where id = #{aid}")
    int incrementRCount(@Param("aid")int aid);


    @Select("select * from articleinfo order by id desc limit #{psize} offset #{offset}")
    List<Articleinfo> getListByPage(@Param("psize")int psize, @Param("offset")int offset);

    @Select("select count(*) from articleinfo")
    int getArtCount();

    @Update("update articleinfo set title = #{title}, type = #{type}, time = #{time}, updatetime = #{updatetime} where id = #{id}")
    int updateBook(Articleinfo articleinfo);

    @Select("select * from articleinfo where title = #{title} order by id desc")
    List<Articleinfo> getArticleByTitle(@Param("title") String title);

    @Select("select * from articleinfo where type = #{type} order by id desc")
    List<Articleinfo> getArticleByType(@Param("type") String type);

    @Select("select * from articleinfo where time = #{time} order by id desc")
    List<Articleinfo> getArticleByTime(@Param("time") String time);

    @Select("select * from discussinfo where aid = #{aid}")
    List<DiscussInfo> getDiscussByAid(@Param("aid")int aid);
    
    @Select("select * from replyinfo where did = #{did};")
    List<ReplyInfo> getReplyByDid(@Param("did")int did);

    @Insert("insert into discussinfo(user,discuss,aid) values (#{user}, #{discuss},#{aid})")
    int doDiscuss(DiscussInfo discussInfo);

    @Select("select id from discussinfo where user = #{toUser} and discuss = #{discuss}")
    Integer getIdByUserAndDiscuss(ReplyInfoVO replyInfoVO);

    @Insert("insert into replyinfo(user,reply,did) values (#{user}, #{reply}, #{did})")
    int addReply(ReplyInfo replyInfo);

    @Select("select * from articleinfo")
    List<Articleinfo> getArticleAll();

    @Select("select uid from articleinfo where id = #{id}")
    Integer getUidByAid(@Param("id") Integer id);
}
